import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

import _ from 'lodash'

import store from '@/store'
import type {StateAll} from "@/store";

const Login = () => import('@/views/Login/Login.vue')
const Home = () => import('@/views/Home/Home.vue')
const Apply = () => import('@/views/Apply/Apply.vue')
const Check = () => import('@/views/Check/Check.vue')
const Sign = () => import('@/views/Sign/Sign.vue')
const Exception = () => import('@/views/Exception/Exception.vue')
const NotAuth = () => import('@/views/NotAuth/NotAuth.vue');
const NotFound = () => import('@/views/NotFound/NotFound.vue');
const NotServer = () => import('@/views/NotServer/NotServer.vue');
declare module 'vue-router'{
    interface RouteMeta {
        menu?:boolean
        title?: string
        icon?: string
        auth?: boolean
    }
}

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        component: Home,
        redirect: '/sign',
        meta: {
            menu: true,
            title: '考勤管理',
            icon: 'document-copy',
            auth: true
        },
        children: [
            {
                path: 'sign',
                name: 'sign',
                component: Sign,
                meta: {
                    menu: true,
                    title: '在线打卡签到',
                    icon: 'calendar',
                    auth: true
                },
                async beforeEnter(to,from,next){
                    const usersInfos = (store.state as StateAll).users.infos;
                    const signsInfos = (store.state as StateAll).signs.infos;
                    const newsInfo = (store.state as StateAll).news.info;
                    if( _.isEmpty(signsInfos) ){
                        const res = await store.dispatch('signs/getTime', { userid: usersInfos._id })
                        if(res.data.errcode === 0){
                            store.commit('signs/updateInfos', res.data.infos)
                        }
                        else{
                            return;
                        }
                    }
                    if( _.isEmpty(newsInfo) ){
                        const res = await store.dispatch('news/getRemind', { userid: usersInfos._id })
                        if(res.data.errcode === 0){
                            store.commit('news/updateInfo', res.data.info)
                        }
                        else{
                            return;
                        }
                    }
                    next()
                }
            },
            {
                path: 'exception',
                name: 'exception',
                component: Exception,
                meta: {
                    menu: true,
                    title: '异常考勤查询',
                    icon: 'warning',
                    auth: true,
                },
                async beforeEnter(to, from, next){
                    const usersInfos = (store.state as StateAll).users.infos;
                    const signsInfos = (store.state as StateAll).signs.infos;
                    const checksApplyList = (store.state as StateAll).checks.applyList;
                    const newsInfo = (store.state as StateAll).news;
                    if( _.isEmpty(signsInfos) ){
                        const res = await store.dispatch('signs/getTime', { userid: usersInfos._id })
                        if(res.data.errcode === 0){
                            store.commit('signs/updateInfos', res.data.infos)
                        }
                        else{
                            return;
                        }
                    }
                    if( _.isEmpty(checksApplyList) ){
                        const res = await store.dispatch('checks/getApply', { applicantid: usersInfos._id })
                        if(res.data.errcode === 0){
                            store.commit('checks/updateApplyList', res.data.rets)
                        }
                        else{
                            return;
                        }
                    }
                    if( _.isEmpty(newsInfo) ){
                        const res = await store.dispatch('news/getRemind', { userid: usersInfos._id })
                        if(res.data.errcode === 0){
                            store.commit('news/updateInfo', res.data.info)
                        }
                        else{
                            return;
                        }
                    }
                    next()
                }
            },
            {
                path: 'apply',
                name: 'apply',
                component: Apply,
                meta: {
                    menu: true,
                    title: '添加考勤审批',
                    icon: 'document-add',
                    auth: true,
                },
                async beforeEnter(to, from, next){
                    const usersInfos = (store.state as StateAll).users.infos;
                    const checksApplyList = (store.state as StateAll).checks.applyList;
                    const newsInfo = (store.state as StateAll).news.info;
                    if( _.isEmpty(checksApplyList) ){
                        const res = await store.dispatch('checks/getApply', { applicantid: usersInfos._id })
                        if(res.data.errcode === 0){
                            store.commit('checks/updateApplyList', res.data.rets)
                        }
                        else{
                            return;
                        }
                    }
                    if( newsInfo.applicant ){
                        const res = await store.dispatch('news/putRemind', { userid: usersInfos._id, applicant: false })
                        if(res.data.errcode === 0){
                            store.commit('news/updateInfo', res.data.info)
                        }
                        else{
                            return;
                        }
                    }
                    next()
                }
            },
            {
                path: 'check',
                name: 'check',
                component: Check,
                meta: {
                    menu: true,
                    title: '我的考勤审批',
                    icon: 'finished',
                    auth: true,
                },
                async beforeEnter(to, from, next){
                    const usersInfos = (store.state as StateAll).users.infos;
                    const checksCheckList = (store.state as StateAll).checks.checkList;
                    const newsInfo = (store.state as StateAll).news.info;
                    if( _.isEmpty(checksCheckList) ){
                        const res = await store.dispatch('checks/getApply', { approverid: usersInfos._id })
                        if(res.data.errcode === 0){
                            store.commit('checks/updateCheckList', res.data.rets)
                        }
                        else{
                            return;
                        }
                    }
                    if( newsInfo.approver ){
                        const res = await store.dispatch('news/putRemind', { userid: usersInfos._id, approver: false })
                        if(res.data.errcode === 0){
                            store.commit('news/updateInfo', res.data.info)
                        }
                        else{
                            return;
                        }
                    }
                    next()

                }
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/403',
        name: 'notAuth',
        component: NotAuth
    },
    {
        path: '/404',
        name: 'notFound',
        component: NotFound
    },
    {
        path: '/500',
        name: 'notServer',
        component: NotServer
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/404'
    }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
    // debugger;
    const token = (store.state as StateAll).users.token
    const infos = (store.state as StateAll).users.infos

    //访问有权限的页面且infos为空
    if(to.meta.auth && _.isEmpty(infos) ){
        //如果有token
        if(token){
            //获取用户信息
            store.dispatch('users/infos').then((res)=>{
                //console.log(res)
                if(res.data.errcode === 0){
                    //修改用户信息
                    store.commit('users/updateInfos', res.data.infos)
                    if(res.data.infos.permission.includes(to.name)){
                        next()
                    }{
                        next('/403')
                    }

                }
            })
            //没有token 直接重定向会login
        }else {
            next('/login')
        }
        //如果infos不为空或没有权限
    }else{
        //如果有token且访问的是 login 则跳转首页
        if(token && to.path === '/login'){
            next('/')
            //否则正常访问
        }else{
            next()
        }
    }
})

export default router
